
***********************************************
* Authors: Henrik Andersson & Sirus Dehdari
* This do-file generates Figure C3 in the Appendix
*****************************************************

clear all
set more off

cd "D:\STR_RiskScores\Stata"


****************
* PREPARE DATA *
****************

*2014 precinctinfo
use "NomVald\Valdist_AV"
keep if Ar==2014
rename llkkdddd precinct2014
keep LopNr precinct2014

save "E:\ProjData\IntegrationSD\temp\2014temp", replace

///surveydata

use "NomVald\Valdist_AV"

*Keep only 2010
keep if Ar==2010
rename llkkdddd precinct2010
keep LopNr precinct2010

merge 1:1 LopNr using "STRSalty\slty_map"
keep if _merge==3
drop _merge

*duplicates report LopNr - OK

///merge on some socioeconomic data

merge 1:1 LopNr using "LISA\LISA2010"
rename ArbstId_LopNr ArbstId 

keep if _merge==3 /*1 obs dropped*/
drop _merge


///rtb - demographic data

merge 1:1 LopNr using "RTB\RTB2009"

keep if _merge==3
drop _merge

merge 1:1 LopNr using "FlerGen\FodelseUppg"

keep if _merge==3
drop _merge

merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2014temp"
keep if _merge==3
drop _merge 

erase "E:\ProjData\IntegrationSD\temp\2014temp.dta"

*******************************
* MERGE ON PRECINCT INFO 2010 *
*******************************
rename precinct2014 precinct2

*string the 2010 precinct, but call it 2014 since it is called that in mergin data
gen str8 precinct2014 = string(precinct2010,"%08.0f")
drop precinct2010

merge m:1 precinct2014 using "E:\ProjData\IntegrationSD\temp\reg_2010_2014.dta", keepusing(share_im_cworker_swe other pop)


keep if _merge==3
drop _merge

*Now rename the 2010 precinct to 2010
rename precinct2014 precinct2010

gen str8 llkkdddd = string(precinct2,"%08.0f")
drop precinct2
rename share_im_cworker_swe share_im_cworker_swe_2010

merge m:1 llkkdddd using "E:\ProjData\IntegrationSD\temp\2014.dta", keepusing(share_im_cworker_swe other pop)

keep if _merge==3
drop _merge

rename llkkdddd precinct2014
rename share_im_cworker_swe share_im_cworker_swe_2014

rename ATTITYD33_26 refugee_opinion
drop if refugee_opinion==9 /*missing*/


*Comparison
gen changecworkers = share_im_cworker_swe_2014-share_im_cworker_swe_2010

rename ATTITYD33_24 language_test  

gen refugee_discrete = 1 if refugee_opinion==4 | refugee_opinion==5
replace refugee_discrete=0 if refugee_opinion==1 | refugee_opinion==2

save  "E:\ProjData\IntegrationSD\temp\correlations.dta", replace



*Tester
corr refugee_opinion changecworkers if Yrkstalln>0

drop if Yrkstalln==0

bysort refugee_opinion: sum(changecworkers)


logit refugee_discrete changecworkers, robust
reg refugee_discrete changecworkers, robust

label variable changecworkers "Change in precinct mean $\#$ co-workers born outside Europe, 2010-2014"
label variable refugee_discrete "Attitude toward refugee policy 20009"
ttest changecworkers, by(refugee_discrete)

estpost ttest changecworkers, by(refugee_discrete)
esttab using "C:\Userdata\Shared\Output\IntegrationSD\t-test.tex", /// 
wide float label replace 
esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\t-test.tex", /// 
float label wide replace 

gen other_share = other/pop

collapse (mean) changecworkers other_share (count) n=changecworkers n2=other_share  ///
(sd) sd=changecworkers sd2=other_share, by(refugee_opinion)

*Confidence intervals
drop if refugee_opinion==.
gen margin= 1.96*(sd/sqrt(n))

gen upper = changecworkers+margin
gen lower = changecworkers-margin

twoway rcap upper lower refugee_opinion, lstyle(ci) || /// 
scatter changecworkers refugee_opinion , mstyle(pi) ///
graphregion(color(white)) xtitle("Attitude toward refugee policy 2009") ///
ytitle("Change precinct foreign co-workers, 2010-2014") ///
legend(off)
graph export "C:\Userdata\Shared\Output\IntegrationSD\corridentidication.pdf", replace
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Figures\corridentidication.pdf", replace




